###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Expire.pm.pl -> Expire module                                  #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::Expire;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "Logs.pm.pl";
require "StaticPages.pm.pl";

use strict;

require "ControlPanel/Output/Expire.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "EXPIRE");

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::Expire->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show();
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "EXPIRE");

  #----------------------------------------------------------------------#
  # Preparing data...                                                    #

  my $Skin        = Skin::CP::Expire->new();
  my $StaticPages = StaticPages->new();

  &Standard::PrintHTMLHeader();  

  unless ($SL::QUERY{'do'}) {
    print $Skin->do();
    return 1;
  }

  my $Log = Logs->new(PRINT => 1);

  my $Exit = eval {
    sub {
      $Log->Footer();
      exit;
    }
  };

  $Log->Header();

  $Log->Line("", "uplevel");

  if ($SL::QUERY{'FORM_REMOVE'}) {
    $Log->Line("Beginning removal process.\n\n", "element");
    
    $Log->Line("Removing category static pages...", "element");
    $Log->Line("", "uplevel");
    
    opendir(CATEGORIES, "$SYSTEM->{'PUBLIC_PATH'}/Static/Category");
    foreach my $file (readdir(CATEGORIES)) {
      next unless ($file =~ /\.(html|shtml|del)$/);
      $Log->Line("Removing $file... ", "element");
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$file") || ($Log->Line("Error: $!", "error") and &$Exit);
      $Log->Line("Done.", "success");
    }
    closedir(CATEGORIES);
    
    $Log->Line("", "downlevel");
    $Log->Line("Removing resource detail static pages...", "element");
    $Log->Line("", "uplevel");
    
    opendir(DETAIL, "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail");
    foreach my $file (readdir(DETAIL)) {
      next unless ($file =~ /\.(html|shtml|del)$/);
      $Log->Line("Removing $file... ", "element");
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$file") || ($Log->Line("Error: $!", "error") and &$Exit);
      $Log->Line("Done.", "success");
    }
    closedir(DETAIL);
    
    $Log->Line("", "downlevel");
    
    $Log->Line("Removing Index static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Index.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Index.html") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Index.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Index.del") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    $Log->Line("Done.", "success");

    $Log->Line("Removing What's Cool static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.html") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.del") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    $Log->Line("Done.", "success");

    $Log->Line("Removing What's Hot static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.html") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.del") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    $Log->Line("Done.", "success");

    $Log->Line("Removing What's New static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/New.html") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/New.html") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/New.del") {
      unlink("$SYSTEM->{'PUBLIC_PATH'}/Static/New.del") || ($Log->Line("Error: $!", "error") and &$Exit);
    }
    $Log->Line("Done.", "success");
  } else {
    $Log->Line("Beginning expiry process.\n\n", "element");

    $Log->Line("Expiring category static pages...", "element");
    $Log->Line("", "uplevel");
    
    opendir(CATEGORIES, "$SYSTEM->{'PUBLIC_PATH'}/Static/Category");
    foreach my $file (readdir(CATEGORIES)) {
      next unless ($file =~ /^(.*?)\-(.*?)\.(s)?html$/);
      $Log->Line("Expiring $file... ", "element");
      unless (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$1-$2.del") {
        open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Category/$1-$2.del") || ($Log->Line("Error: $!", "error") and &$Exit);
        close(FILE);
      }
      $Log->Line("Done.", "success");
    }
    closedir(CATEGORIES);
    
    $Log->Line("", "downlevel");
    $Log->Line("Expiring resource detail static pages...", "element");
    $Log->Line("", "uplevel");
    
    opendir(DETAIL, "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail");
    foreach my $file (readdir(DETAIL)) {
      next unless ($file =~ /^(.*?)\.(s)?html$/);
      $Log->Line("Expiring $file... ", "element");
      unless (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$1.del") {
        open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Detail/$1.del") || ($Log->Line("Error: $!", "error") and &$Exit);
        close(FILE);
      }
      $Log->Line("Done.", "success");
    }
    closedir(DETAIL);
    
    $Log->Line("", "downlevel");
    
    $Log->Line("Expiring Index static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Index.html" && !-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Index.del") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Index.del") || ($Log->Line("Error: $!", "error") and &$Exit);
      close(FILE);
    }
    $Log->Line("Done.", "success");

    $Log->Line("Expiring What's Cool static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.html" && !-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.del") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Cool.del") || ($Log->Line("Error: $!", "error") and &$Exit);
      close(FILE);
    }
    $Log->Line("Done.", "success");

    $Log->Line("Expiring What's Hot static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.html" && !-e "$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.del") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/Hot.del") || ($Log->Line("Error: $!", "error") and &$Exit);
      close(FILE);
    }
    $Log->Line("Done.", "success");

    $Log->Line("Expiring What's New static page...", "element");
    if (-e "$SYSTEM->{'PUBLIC_PATH'}/Static/New.html" && !-e "$SYSTEM->{'PUBLIC_PATH'}/Static/New.del") {
      open(FILE, ">$SYSTEM->{'PUBLIC_PATH'}/Static/New.del") || ($Log->Line("Error: $!", "error") and &$Exit);
      close(FILE);
    }
    $Log->Line("Done.", "success");
  }

  $Log->Line("\n\n", "");
  $Log->Line("All done!", "element");
  $Log->Line("", "downlevel");

  &$Exit;
}

1;